package com.genshang.restaurant.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.genshang.restaurant.entity.Genshanguserlist;
import com.genshang.restaurant.entity.Rolelist;
import com.genshang.restaurant.mapper.GenshanguserlistMapper;
import com.genshang.restaurant.result.ResultBody;
import com.genshang.restaurant.service.IGenshanguserlistService;
import com.genshang.restaurant.service.IRolelistService;
import com.genshang.restaurant.util.EncryptSHAUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Alex
 * @since 2020-09-26
 */
@Service
public class GenshanguserlistServiceImpl extends ServiceImpl<GenshanguserlistMapper, Genshanguserlist> implements IGenshanguserlistService {

    @Autowired
    private IRolelistService rolelistService;

    @Override
    public ResultBody login(String nickname, String password) {

        if (null == nickname || null == password) {
            return ResultBody.error("参数为空");
        }
        password = EncryptSHAUtil.sha256(password);
        QueryWrapper<Genshanguserlist> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("NICKNAME", nickname);
        queryWrapper.eq("PASSWORD", password);
        List<Genshanguserlist> genshanguserlistList = this.list(queryWrapper);
        if (CollectionUtils.isEmpty(genshanguserlistList)) {
            return ResultBody.error("登录失败");
        }
        Genshanguserlist genshanguserlist = genshanguserlistList.get(0);
        Rolelist rolelist = rolelistService.getRolelistByName(genshanguserlist.getRolenamelist());
        Map resultMap = JSON.parseObject(JSON.toJSONString(genshanguserlist), Map.class);
        resultMap.put("rolelist", rolelist);
        return ResultBody.success(resultMap);
    }
}
